package pe.com.rix.taglib.view;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.JspFragment;
import javax.servlet.jsp.tagext.SimpleTagSupport;

import pe.com.cineplanet.bean.User;
import pe.com.cineplanet.cons.Const;

public class Session extends SimpleTagSupport {

	private String key;
	
	@Override
	public void doTag() throws JspException, IOException {
	
		//String id = (String)getJspContext().getAttribute("id", PageContext.SESSION_SCOPE);
		User user = (User)getJspContext().getAttribute(Const.SESSION_KEY, PageContext.SESSION_SCOPE);
		
		JspWriter out = getJspContext().getOut();
		
		String value = "";
		if(user==null || user.isCliente()){
			value = "Visitante";
		}else{
			if(getKey().trim().equalsIgnoreCase("user")){
				value = user.getNames() + " " + user.getLastnames();
			}else if(getKey().trim().equalsIgnoreCase("role")){
				value = user.getRole().getName();
			}else if(getKey().trim().equalsIgnoreCase("company")){
				value = user.getRole().getCompany().getName();
			}else if(getKey().trim().equalsIgnoreCase("locale")){
				value = user.getRole().getCompany().getLocale();
			}else if(getKey().trim().equalsIgnoreCase("email")){
				value = user.getEmail();
			}		
		}
		
		try {
			JspFragment f = getJspBody();
			if (f != null) {
				f.invoke(out);
			}
			out.print(value);
		} catch (java.io.IOException ex) {
			throw new JspException(ex.getMessage());
		}
	}
	
	public String getKey() {
		return key;
	}
	
	public void setKey(String key) {
		this.key = key;
	}
	
	
}
